﻿using nplJobsCommon.Common;
using nplJobsCommon.JobSeeker;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
    
namespace nplJobsDAL.Insert
{
    public class InsertJobAlert : DataAccessBase
    {
        public int lastJobAlertID = 0;
        public JobAlert JobAlert { get; set; }

        public InsertJobAlert()
        {
            StoredProcedureName = StoredProcedure.Name.spInsertJobAlert.ToString();
        }

        public void Insert()
        {
            InsertJobAlertDataParameters insertJobAlertDataParameters = new InsertJobAlertDataParameters(this.JobAlert);
            DataBaseHelper dbHelper = new DataBaseHelper(StoredProcedureName);
            dbHelper.Parameters = insertJobAlertDataParameters.Parameters;
            object lastJobID = dbHelper.RunScalar();

            if (lastJobID != null)
            {
                this.lastJobAlertID = int.Parse(lastJobID.ToString());
            }
        }
    }

    #region SQL parameters for job alert
    public class InsertJobAlertDataParameters
    {
        private SqlParameter[] _parameters;
        public int lastJobAlertID;

        public InsertJobAlertDataParameters(JobAlert jobAlert)
        {
            this.JobAlert = jobAlert;
            Build();
        }
        
        private void Build()
        {
            SqlParameter[] parameters = 
                {
                    new SqlParameter("@JobAlertID", JobAlert.JobAlertID),
                    new SqlParameter("@JobSeekerID", JobAlert.JobSeekerID),
                    new SqlParameter("@AlertName", JobAlert.AlertName),
                    new SqlParameter("@FrequencyID", JobAlert.FrequencyID),
                    new SqlParameter("@Keyword", JobAlert.Keyword),
                    new SqlParameter("@Skills", JobAlert.Skills),
                    new SqlParameter("@CategoryID", JobAlert.CategoryID),
                    new SqlParameter("@CountryID", JobAlert.CountryID),
                    new SqlParameter("@CityID", JobAlert.CityID),
                    new SqlParameter("@WorkTypeID", JobAlert.WorkTypeID),
                    new SqlParameter("@SalaryRangeID", JobAlert.SalaryRangeID),
                    new SqlParameter("@UpdatedDate", JobAlert.UpdatedDate),
                    new SqlParameter("@IsActive", JobAlert.IsActive),
                    new SqlParameter("@LastJobAlertID", lastJobAlertID){ Direction= System.Data.ParameterDirection.ReturnValue},
                };

            Parameters = parameters;
        }

        public JobAlert JobAlert { get; set; }

        public SqlParameter[] Parameters
        {
            get { return _parameters; }
            set { _parameters = value; }
        }
    }
    #endregion
}
